package com.javacoo.junit.generator.logging;



import static com.javacoo.junit.generator.internal.util.messages.Messages.getString;

/**
 * 日志对象创建工厂
 * <li></li>
 * @author duanyong@jccfc.com
 * @date 2020/12/31 15:26
 */
public class LogFactory {
    private static AbstractLogFactory theFactory;
    public static final String MARKER = "MYBATIS-GENERATOR";

    // static {
    //     tryImplementation(new Slf4jLoggingLogFactory());
    //     tryImplementation(new JakartaCommonsLoggingLogFactory());
    //     tryImplementation(new Log4j2LoggingLogFactory());
    //     tryImplementation(new Log4jLoggingLogFactory());
    //     tryImplementation(new Jdk14LoggingLogFactory());
    //     tryImplementation(new NoLoggingLogFactory());
    // }

    private LogFactory() {}

    public static Log getLog(Class<?> clazz) {
        try {
            return theFactory.getLog(clazz);
        } catch (Exception t) {
            throw new RuntimeException(getString("RuntimeError.21",clazz.getName(), t.getMessage()), t);
        }
    }

    public static void setLogFactory(AbstractLogFactory logFactory) {
        setImplementation(logFactory);
    }

    private static void tryImplementation(AbstractLogFactory factory) {
        if (theFactory == null) {
            try {
                setImplementation(factory);
            } catch (LogException e) {
                // ignore
            }
        }
    }

    private static void setImplementation(AbstractLogFactory factory) {
        try {
            Log log = factory.getLog(LogFactory.class);
            if (log.isDebugEnabled()) {
                log.debug("Logging initialized using '" + factory + "' adapter.");
            }
            theFactory = factory;
        } catch (Throwable t) {
            throw new LogException("Error setting Log implementation.  Cause: " + t.getMessage(), t);
        }
    }
}
